package com.guaju.yueshangLife.activity.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AbsoluteLayout;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.activity.CaculateActivity;
import com.guaju.yueshangLife.activity.CancleOrderReasonDialogActivity;
import com.guaju.yueshangLife.activity.CommentGoodsListActivity;
import com.guaju.yueshangLife.activity.DeliveryDetailActivity;
import com.guaju.yueshangLife.activity.OrderPaySelectMethodActivity;
import com.guaju.yueshangLife.activity.OrderPaySuccessOfflineActivity;
import com.guaju.yueshangLife.adapter.OrderDetailInnerAdapter;
import com.guaju.yueshangLife.base.BaseActivity;
import com.guaju.yueshangLife.bean.CancleOrderBean;
import com.guaju.yueshangLife.bean.MineOrder;
import com.guaju.yueshangLife.bean.OrderDetailBean;
import com.guaju.yueshangLife.bean.OrderJudgeUtil;
import com.guaju.yueshangLife.bean.ReceiveGoods;
import com.guaju.yueshangLife.databinding.OrderDetailActivityBinding;
import com.guaju.yueshangLife.global.ConstantsValue;
import com.guaju.yueshangLife.net.ApiEntity;
import com.guaju.yueshangLife.utils.DateUtils;
import com.guaju.yueshangLife.utils.ToastUtil;
import com.guaju.yueshangLife.utils.Utils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrderDetailActivity extends BaseActivity {

    public static final String REQUEST_REASON_SELECTED = "cancle_reason";
    public static final String REQUEST_REASON = "reason";
    public static final String ORDER_ID = "orderId";
    public int selectedReason = -1;

    private static final int REQUEST_SELECT_REASON = 1800;

    private OrderDetailActivityBinding binding;
    private int order_id;
    private String created_at;
    private List<OrderDetailBean.DataBean.ShipDataBean.DetailBean> deliveryList;
    private MaterialDialog confirmDialog;
    private String exp;
    private String exp_no;
    private int payment_method;
    private OrderDetailBean.DataBean data;
    private OrderDetailBean.DataBean.ExtraBean.OfflineBean offline;
    private View news_loading;
    private int cancel_at;
    private String total_account;
    private MaterialDialog deleteDialog;

    @Override
    protected void loadViewLayout() {
        binding = DataBindingUtil.setContentView(this, R.layout.order_detail_activity);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void bindViews() {
        addProgressPic();

    }

    public void addProgressPic() {
        news_loading = View.inflate(OrderDetailActivity.this, R.layout.loading, null);
        news_loading.setLayoutParams(new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.MATCH_PARENT, AbsoluteLayout.LayoutParams.MATCH_PARENT, 0, 0));
        Bitmap mBgBitmap = Utils.readBitMap(OrderDetailActivity.this, R.drawable.loading_order_detail);
        news_loading.setBackgroundDrawable(new BitmapDrawable(mBgBitmap));
        binding.rlContainer.addView(news_loading);
        news_loading.setVisibility(View.VISIBLE);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        order_id = intent.getIntExtra("order_id", 0);
        ApiEntity.orderDetail(order_id);
    }

    @Override
    protected void setListener() {


        binding.btComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OrderDetailActivity.this, CommentGoodsListActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("orderId", order_id);
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });

        binding.btCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCancleOrderDialog(order_id);
            }
        });
        binding.btCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = binding.tvOrderNum.getText().toString().trim();
                Utils.copyLabel(OrderDetailActivity.this, trim);
            }
        });

        binding.btDelivery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**
                 * 物流
                 */
                Intent intent = new Intent(OrderDetailActivity.this, DeliveryDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("delivery", (Serializable) deliveryList);
                bundle.putString("number", exp_no);
                bundle.putString("exp", exp);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        binding.btConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showConfirmOrderDialog(order_id);
            }
        });

        binding.btPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (payment_method == 0||payment_method == 1) {
                    Intent intent = new Intent(OrderDetailActivity.this, OrderPaySuccessOfflineActivity.class);
                    Bundle bundle = new Bundle();
                    if (null != offline) {
                        bundle.putString(OrderPaySuccessOfflineActivity.ORDER_NUMBER, offline.getPay_no());
                    }
                    bundle.putInt(OrderPaySuccessOfflineActivity.TOTAL_AMOUNT, data.getTotal_amount());
                    bundle.putString(OrderPaySuccessOfflineActivity.TOTAL_FEE, data.getTotal_account());
                    bundle.putInt(OrderPaySuccessOfflineActivity.ORDER_ID, data.getId());
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    /**
                     * 其他支付方式
                     */
                    Intent intent = new Intent(OrderDetailActivity.this, OrderPaySelectMethodActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putLong(OrderPaySelectMethodActivity.CANCLE_AT, cancel_at);
                    bundle.putString(OrderPaySelectMethodActivity.TOTAL_MONEY, total_account);
                    bundle.putInt(OrderPaySelectMethodActivity.ORDER_ID, order_id);
                    String paymethod = binding.tvPayMethod.getText().toString().trim();
                    bundle.putString(OrderPaySelectMethodActivity.PAY_METHOD, paymethod);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });

        binding.btCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCancleOrderDialog(order_id);
            }
        });
        binding.btDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDeleteOrderDialog(order_id);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOrderDetailReceived(OrderDetailBean bean) {
        if (bean == null) {
            showEmpty();
        } else {
            data = bean.getData();
            OrderDetailBean.DataBean.AddressBean address = data.getAddress();
            created_at = data.getCreated_at();
            OrderDetailBean.DataBean.CustomerBean customer = data.getCustomer();
            OrderDetailBean.DataBean.ExtraBean extra = data.getExtra();

            offline = extra.getOffline();
            int id = data.getId();
            List<OrderDetailBean.DataBean.ItemsBean> items = data.getItems();
            String paid_at = data.getPaid_at();
            payment_method = data.getPayment_method();
            String refund_status = data.getRefund_status();
            Object remark = data.getRemark();
            OrderDetailBean.DataBean.ShipDataBean ship_data = data.getShip_data();
            total_account = data.getTotal_account();
            int total_amount = data.getTotal_amount();
            /**
             * 订单状态
             */
            String status = data.getStatus();
            binding.tvOrderStatus.setText(status);
            /**
             * 未付款
             */
            if (TextUtils.equals(ConstantsValue.UNPAY, status)) {
                binding.llSecondLine.setVisibility(View.VISIBLE);
                binding.llBottom.setVisibility(View.VISIBLE);

                if (0 == payment_method) {
                    binding.llOfflinePay.setVisibility(View.VISIBLE);
                    binding.llPayMethod.setVisibility(View.GONE);
                    binding.llPayAt.setVisibility(View.GONE);
                    if (offline != null) {
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.GONE);
                    binding.btPay.setVisibility(View.VISIBLE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.GONE);
                }
                binding.btCancle.setVisibility(View.VISIBLE);
                binding.btComment.setVisibility(View.GONE);
                binding.btDel.setVisibility(View.GONE);
                binding.btDelivery.setVisibility(View.GONE);
                binding.btConfirm.setVisibility(View.GONE);

                binding.tvService.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        /**
                         * 客服
                         */
                    }
                });


                cancel_at = data.getCancel_at();
                long now = System.currentTimeMillis() / 1000;
                long l = cancel_at - now;
                if (l > 0) {
                    String distance = DateUtils.getDistance(l);
                    binding.tvOrderSecond.setText("剩余" + distance + "系统将自动取消订单");
                } else {
                    binding.tvOrderSecond.setText("订单超时");
                }

            } else if (TextUtils.equals(ConstantsValue.WAIT_FOR_PAY, status)) {
                binding.llBottom.setVisibility(View.GONE);
                binding.llSecondLine.setVisibility(View.GONE);
                binding.llPayAt.setVisibility(View.GONE);
                if (0 == payment_method) {
                    if (offline != null) {
                        binding.llOfflinePay.setVisibility(View.VISIBLE);
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.llOfflinePay.setVisibility(View.VISIBLE);
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.GONE);
                    binding.btPay.setVisibility(View.VISIBLE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.GONE);
                }
            }
            /**
             * 完成
             */
            else if (TextUtils.equals(ConstantsValue.DONE, status)) {
                binding.llSecondLine.setVisibility(View.GONE);
                binding.llBottom.setVisibility(View.GONE);
                binding.llDelivery.setVisibility(View.VISIBLE);
                binding.llPayAt.setVisibility(View.VISIBLE);
                binding.llPayMethod.setVisibility(View.VISIBLE);
                if (0 == payment_method) {
                    binding.llOfflinePay.setVisibility(View.VISIBLE);
                    if (offline != null) {
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.GONE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.VISIBLE);
                }
            }
            /**
             * 取消
             */
            else if (TextUtils.equals(ConstantsValue.CANCLE, status)) {

                binding.btPay.setVisibility(View.GONE);
                binding.btCancle.setVisibility(View.GONE);
                binding.btComment.setVisibility(View.GONE);
                binding.btDel.setVisibility(View.VISIBLE);
                binding.btDelivery.setVisibility(View.GONE);
                binding.btConfirm.setVisibility(View.GONE);


                binding.llOfflinePay.setVisibility(View.GONE);
                binding.llBottom.setVisibility(View.VISIBLE);
                binding.llSecondLine.setVisibility(View.GONE);
                binding.btDel.setVisibility(View.VISIBLE);
                if (0 == payment_method) {
                    if (offline != null) {
                        binding.llOfflinePay.setVisibility(View.VISIBLE);
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.llOfflinePay.setVisibility(View.GONE);
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.VISIBLE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.VISIBLE);
                }
            } else if (TextUtils.equals(ConstantsValue.REFUND, status)) {
                binding.llBottom.setVisibility(View.VISIBLE);
                if (0 == payment_method) {
                    if (offline != null) {
                        binding.llOfflinePay.setVisibility(View.VISIBLE);
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.llOfflinePay.setVisibility(View.VISIBLE);
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.GONE);
                    binding.btPay.setVisibility(View.VISIBLE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.VISIBLE);
                }

            } else if (TextUtils.equals(ConstantsValue.UNCOMMENT, status)) {

                binding.llSecondLine.setVisibility(View.GONE);
                binding.llBottom.setVisibility(View.VISIBLE);
                binding.llDelivery.setVisibility(View.VISIBLE);
                binding.llPayAt.setVisibility(View.VISIBLE);
                binding.llPayMethod.setVisibility(View.VISIBLE);

                if (null == ship_data) {
                    binding.btDelivery.setVisibility(View.GONE);
                } else {
                    binding.btDelivery.setVisibility(View.VISIBLE);
                }
                binding.btPay.setVisibility(View.GONE);
                binding.btCancle.setVisibility(View.GONE);
                binding.btComment.setVisibility(View.VISIBLE);
                binding.btDel.setVisibility(View.VISIBLE);
                binding.btConfirm.setVisibility(View.GONE);
                binding.btDelivery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(OrderDetailActivity.this, DeliveryDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("delivery", (Serializable) deliveryList);
                        bundle.putString("number", exp_no);
                        bundle.putString("exp", exp);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                boolean isAllCommended = true;

                for (OrderDetailBean.DataBean.ItemsBean bean1 : items) {
                    if (bean1.getIs_review() != 1) {
                        isAllCommended = false;
                    }
                }
                if (isAllCommended) {
                    binding.btComment.setBackgroundResource(R.drawable.shape_add_cart_unable);
                    binding.btComment.setTextColor(Color.parseColor("#666666"));
                    binding.btComment.setText("已评价");
                } else {
                    binding.btComment.setBackgroundResource(R.drawable.shape_add_cart);
                    binding.btComment.setTextColor(Color.parseColor("#6500AC"));
                    binding.btComment.setText("评价");
                    binding.btComment.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(OrderDetailActivity.this, CommentGoodsListActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putInt("orderId", order_id);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }


                if (1 == payment_method) {
                    binding.llOfflinePay.setVisibility(View.VISIBLE);
                    if (offline != null) {
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.GONE);
                    binding.btPay.setVisibility(View.VISIBLE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.VISIBLE);
                }

                /**
                 * 取消待做
                 */

            } else if (TextUtils.equals(ConstantsValue.UNRECEIVE, status) || TextUtils.equals(ConstantsValue.UNSEND, status)) {
                binding.llBottom.setVisibility(View.VISIBLE);
                binding.llSecondLine.setVisibility(View.GONE);

                binding.btPay.setVisibility(View.GONE);
                binding.btCancle.setVisibility(View.GONE);
                binding.btComment.setVisibility(View.GONE);
                binding.btDel.setVisibility(View.GONE);
                binding.btDelivery.setVisibility(View.VISIBLE);
                binding.btConfirm.setVisibility(View.VISIBLE);

                if (1 == payment_method) {
                    binding.llOfflinePay.setVisibility(View.VISIBLE);
                    if (offline != null) {
                        String pay_no = offline.getPay_no();
                        String pay_bank = offline.getPay_bank();
                        String pay_name = offline.getPay_name();
                        String pay_account = offline.getPay_account();
                        binding.tvPaynumber.setText(pay_no);
                        binding.tvPayname.setText(pay_name);
                        binding.tvPaybank.setText(pay_bank);
                        binding.tvPayacount.setText(pay_account);
                    } else {
                        binding.tvPaynumber.setText("未设置");
                        binding.tvPayname.setText("未设置");
                        binding.tvPaybank.setText("未设置");
                        binding.tvPayacount.setText("未设置");
                    }
                } else {
                    binding.llOfflinePay.setVisibility(View.GONE);
                    binding.llPayMethod.setVisibility(View.VISIBLE);
                    binding.llPayAt.setVisibility(View.VISIBLE);
                }
            }
            if (ship_data != null) {
                exp = ship_data.getExp();
                exp_no = ship_data.getExp_no();
                deliveryList = ship_data.getDetail();
                if (deliveryList != null && !deliveryList.isEmpty()) {
                    OrderDetailBean.DataBean.ShipDataBean.DetailBean detailBean = deliveryList.get(deliveryList.size() - 1);
                    String datetime = detailBean.getDatetime();
                    String remark1 = detailBean.getRemark();
                    binding.tvDeliveryDetail.setText(remark1);
                    binding.tvUpdateTime.setText(datetime);
                } else {
                    binding.tvDeliveryDetail.setText("暂无记录");
                    binding.tvUpdateTime.setText("");
                }
            } else {
                binding.tvDeliveryDetail.setText("暂无记录");
                binding.tvUpdateTime.setText("");
            }
            if (1 == payment_method||0 == payment_method) {
                binding.tvPayMethod.setText("线下转账");
            } else if (2 == payment_method) {
                binding.tvPayMethod.setText("支付宝");
            } else if (3 == payment_method) {
                binding.tvPayMethod.setText("微信");
            } else if (4 == payment_method) {
                binding.tvPayMethod.setText("钱包支付");
            }
            binding.tvPayTime.setText(TextUtils.isEmpty(paid_at) ? "" : paid_at);


            binding.tvTotalPrice.setText(total_account);
            String nameTel = address.getContact_name() + "  " + address.getContact_phone();
            binding.tvNameTel.setText(nameTel);
            binding.tvAddress.setText(address.getAddress());
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(OrderDetailActivity.this);
            binding.recyclerView.setLayoutManager(linearLayoutManager);
            OrderDetailInnerAdapter orderDetailInnerAdapter = new OrderDetailInnerAdapter(OrderDetailActivity.this, items);
            binding.recyclerView.setAdapter(orderDetailInnerAdapter);

            double allPrice = 0.00;
            for (OrderDetailBean.DataBean.ItemsBean itemsBean : items) {
                allPrice += Double.parseDouble(itemsBean.getPrice());
            }
            binding.tvOrderPrice.setText("¥" + allPrice);
            binding.tvGoodsPrice.setText(allPrice + "");

            binding.tvGoodsNum.setText("共" + total_amount + "件商品");
            String postage = extra.getPostage();
            binding.tvDiliveryPrice.setText(postage);


            Object coupon = extra.getCoupon();
            Object invoice = extra.getInvoice();
            if (null == coupon) {
                binding.tvDiscountPrice.setText("0");
            } else {
                ToastUtil.show(coupon.toString());
            }
            if (null == invoice) {
                binding.tvFapiaoCate.setText("不开发票");
            } else {
                ToastUtil.show(invoice.toString());
            }
//            binding.tvOrderNum.setText();
            binding.tvOrderTime.setText(created_at);

        }
        news_loading.setVisibility(View.GONE);

    }

    /**
     * TODO
     * 根据 status  展示不同的状态信息
     *
     * @param status
     */
    private void updateStatus(int status) {

        binding.tvOrderStatus.setText(OrderJudgeUtil.getOrderDetailStatus(status));

        switch (status) {
            case 0:
                binding.llSecondLine.setVisibility(View.VISIBLE);
                binding.tvOrderSecond.setText("超时未付款，系统自动取消");
                binding.llPayMethod.setVisibility(View.GONE);
                binding.llPayAt.setVisibility(View.GONE);
                break;
            case 1:
                binding.llSecondLine.setVisibility(View.VISIBLE);
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = null;
                try {
                    date = format.parse(created_at);
                } catch (ParseException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
                long millis = date.getTime() / 1000;
                Log.e("guajujerry", "updateStatus: " + millis);
                long endTime = millis + 24 * 60 * 60;
                Log.e("guajujerry", "updateStatus: " + endTime);
                long nowTime = System.currentTimeMillis() / 1000;
                Log.e("guajujerry", "updateStatus: " + nowTime);
                long distanceTime = endTime - nowTime;
                if (distanceTime >= 60) {
                    String s = DateUtils.generateTimeFormatte(distanceTime);
                    binding.tvOrderSecond.setText("剩余" + s + "系统将自动取消订单");
                } else {
                    status = 0;
                    updateStatus(status);
                }
                binding.llPayMethod.setVisibility(View.GONE);
                binding.llPayAt.setVisibility(View.GONE);
                break;
            case 2:
                binding.llSecondLine.setVisibility(View.GONE);
                binding.llPayMethod.setVisibility(View.GONE);
                binding.llPayAt.setVisibility(View.GONE);
                break;
            case 3:
                binding.llSecondLine.setVisibility(View.GONE);
                binding.llDelivery.setVisibility(View.VISIBLE);
                binding.llPayAt.setVisibility(View.VISIBLE);
                binding.llPayMethod.setVisibility(View.VISIBLE);

                binding.btDelivery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        /**
                         * 物流
                         */
                        Intent intent = new Intent(OrderDetailActivity.this, DeliveryDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("delivery", (Serializable) deliveryList);
                        bundle.putString("number", exp_no);
                        bundle.putString("exp", exp);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
                binding.btConfirm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showConfirmOrderDialog(order_id);
                    }
                });


                break;
            case 4:
                binding.llSecondLine.setVisibility(View.GONE);
                break;
            case 5:
                binding.llSecondLine.setVisibility(View.VISIBLE);
                binding.tvOrderSecond.setText("还剩x天xx时xx分");
                break;
            case 6:
                binding.llSecondLine.setVisibility(View.VISIBLE);
                binding.tvOrderSecond.setText("还剩x天xx时xx分");
                break;
            case 7:
                binding.llSecondLine.setVisibility(View.GONE);
                break;
            case 8:
                binding.llSecondLine.setVisibility(View.VISIBLE);
                binding.tvOrderSecond.setText("还剩x天xx时xx分");
                break;
            default:
                break;
        }
    }

    public void showCancleOrderDialog(int orderId) {
        Intent intent = new Intent(OrderDetailActivity.this, CancleOrderReasonDialogActivity.class);
        intent.putExtra(REQUEST_REASON_SELECTED, selectedReason);
        intent.putExtra(ORDER_ID, orderId);
        startActivityForResult(intent, REQUEST_SELECT_REASON);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SELECT_REASON) {
            if (data != null) {
                String reason = data.getStringExtra(REQUEST_REASON);
                selectedReason = data.getIntExtra(REQUEST_REASON_SELECTED, -1);
                if (!TextUtils.isEmpty(reason)) {
                    /**
                     * 取消订单
                     */
                    int intExtra = data.getIntExtra(ORDER_ID, -1);
                    if (intExtra != -1) {
                        ApiEntity.cancleOrder(intExtra, reason);
                    }

                }
            } else {
                if (-1 == selectedReason) {
                    ToastUtil.show("未选择取消原因！");
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCancleOrderReceived(CancleOrderBean bean) {
        if (bean == null) {
            ToastUtil.show("内部异常,请稍后重试");
        } else {
            int code = bean.getCode();
            if (200 == code) {
                ToastUtil.show("已取消订单");
            }
        }

    }

    public void showConfirmOrderDialog(final int orderId) {
        confirmDialog = new MaterialDialog.Builder(OrderDetailActivity.this)
                .title("确认收货")
                .content("确认收到该订单商品？")
                .negativeText("确认")
                .negativeColor(getResources().getColor(R.color.c_gray1))
                .positiveText("取消")
                .positiveColor(getResources().getColor(R.color.colorPrimary))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                TODO
                        confirmDialog.dismiss();
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        /**
                         * 确认收货
                         */
                        ApiEntity.receiveOrder(orderId);

                        confirmDialog.dismiss();
                    }
                })
                .build();
        confirmDialog.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(ReceiveGoods bean) {
        if (bean != null) {
            String msg = bean.getMsg();
            if (bean.getCode() == 200) {
                /**
                 * 确认收货
                 */
                finish();
            } else {
                ToastUtil.show(msg);
            }
        }
    }

    public void showDeleteOrderDialog(final int orderId) {
        deleteDialog = new MaterialDialog.Builder(OrderDetailActivity.this)
                .title("确认删除")
                .content("删除后将不能恢复，确认删除？")
                .negativeText("确认")
                .negativeColor(getResources().getColor(R.color.c_gray1))
                .positiveText("取消")
                .positiveColor(getResources().getColor(R.color.colorPrimary))
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
//                                TODO
                        deleteDialog.dismiss();
                    }
                })
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {

                        /**
                         * 删除订单
                         */
                        ApiEntity.deleteOrder(orderId);

                        deleteDialog.dismiss();
                    }
                })
                .build();
        deleteDialog.show();
    }

}
